﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumTime.ViewModels;
using ScrumTime.Services;
using ScrumTime.Models;
using ScrumTime.Helpers;


namespace ScrumTime.Controllers
{
    public class UserController : Controller
    {
        ScrumTimeEntities _ScrumTimeEntities;
        UserService _UserService;


        #region metodos privados auxiliares

        private System.Web.Security.MembershipUser getUser( String username, AccountMembershipService accountService ){
            System.Web.Security.MembershipUser resultado = null;
            foreach (System.Web.Security.MembershipUser user in accountService.GetAllUsers() ) {
                if (username == user.UserName) {
                    resultado = user;
                    break;
                }
            }
            return resultado;
        }

        #endregion


        public UserController()
        {
            _ScrumTimeEntities = new ScrumTimeEntities();
            _UserService = new UserService(_ScrumTimeEntities);            
        }


        [Authorize]
        public ActionResult Index()
        {                       
            UserViewModel userviewmodel = new UserViewModel();
            return View(userviewmodel);
        }

        [Authorize]
        public ActionResult Delete( String username)
        {
            try
            {

                if (User.Identity.Name == username) {
                    throw new Exception("");
                }

                ScrumTimeEntities scrumTimeEntities = new ScrumTimeEntities();
                //ScrumDetail scrumDetails = null;
                var results = from s in scrumTimeEntities.ScrumDetails
                              where s.AssignedTo == username
                              select s;
                if (results.Count() > 0)
                    throw new Exception("");
                else
                {
                    // si no tiene asociado nada, eliminamos y ya
                    AccountMembershipService membershipService = new AccountMembershipService();
                    membershipService.DeleteUser(username);
                }

                return Redirect("index");
            }
            catch {
                return Redirect(ErrorHelper.GetURLGeneralError());  
            }
            
        }


        [Authorize]
        public ActionResult edit(FormCollection collection)
        {
            try
            {
                string newuser = collection.Get("newuser");
                string name = collection.Get("name");
                string mail = collection.Get("mail");
                string password = collection.Get("password");

                AccountMembershipService membershipService = new AccountMembershipService();

                if (newuser.ToLower() == "true")
                {
                    System.Web.Security.MembershipCreateStatus mcs = membershipService.CreateUser(name, password, mail);
                    if (mcs != System.Web.Security.MembershipCreateStatus.Success)
                    {
                        throw new Exception(mcs.ToString());
                    }
                }
                else {
                    System.Web.Security.MembershipUser user = getUser(name, membershipService);
                    user.Email = mail;
                    System.Web.Security.Membership.UpdateUser(user);
                }

                return Redirect("index");
            }
            catch(Exception e )
            {
                return Redirect(ErrorHelper.GetURLErrorWithMessage(e.Message));
            }

        }


        [Authorize]
        public ActionResult data(FormCollection collection)
        {
            try
            {
                // en este caso el gui, es el nombre
                string gui = collection.Get("gui"); 
               
                AccountMembershipService membershipService = new AccountMembershipService();
                System.Web.Security.MembershipUser user =  this.getUser(gui, membershipService);

                return PartialView("data", user);
            }
            catch
            {
                return Redirect(ErrorHelper.GetURLGeneralError());
            }

        }
    
    }
}
